package com.thinkgem.jeesite.modules.sys.web;

import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.OfficeService;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Rocky on 6/17/16.
 */
@Controller
public class RegisterController extends BaseController {

    @Resource
    SystemService systemService;
    @Resource
    OfficeService officeService;

    @RequestMapping(value = "${adminPath}/register",method = RequestMethod.GET)
    public String register(HttpServletRequest request, HttpServletResponse response, Model model){

        return "modules/sys/sysRegister";

    }

    @RequestMapping(value = "${adminPath}/register",method = RequestMethod.POST)
    public String register(String userName,String password,Model model,RedirectAttributes redirectAttributes){

        User user = new User();
        user.setPassword(SystemService.entryptPassword(password));
        user.setLoginName(userName);
        user.getRoleList().add(systemService.getDefaultRole());

        Office company = new Office();
        company.setName("公司名称");
        officeService.save(company);

        user.setCompany(company);
        systemService.saveUser(user);

        addMessage(redirectAttributes, "注册成功");
        return "redirect:" + adminPath + "/a/login";
    }

}
